<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include ('../global/initialize.php');
global $dal;

aos_header('Ruler');

$query = "SELECT faction_id " .
"FROM faction_member " .
"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND invited = 0 AND requested = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if (isset ($row['faction_id'])) {
	$faction_id_tmp = $row['faction_id'];
} else {
	$faction_id_tmp = -1;
}
$query = "SELECT ruler_id, score, experience, start_turn, resigned, user_id, last_year_score, last_year_growth " .
"FROM ruler " .
"WHERE name = '" . formatInput($_GET['name']) . "'";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
?>
    				<div class="blok">
<?php


if (isset ($row['ruler_id'])) {
	$score_tmp = $row['score'];
	$last_year_score = $row['last_year_score'];
	$last_year_growth = round($row['last_year_growth'], 2) . "%";
	$other_user_id = $row['user_id'];
	$start_turn_tmp = $row['start_turn'];
	$battle_score_tmp = $row['experience'];
	$ruler_id_tmp = $row['ruler_id'];
	$user_id_tmp = $row['user_id'];
	if ($last_year_score > 0 && $score_tmp > 0) {
		$growth = round(($score_tmp / $last_year_score -1.0) * 100.0, 2) . "%";
	} else {
		$growth = '-';
	}

	if ($row['resigned'] == 1) {
		$resigned = " (" . translate("resigned") . ")";
	} else {
		$resigned = "";
	}

	$query = "SELECT name, type_name, faction.faction_id " .
	"FROM faction " .
	"JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id " .
	"JOIN faction_member ON faction.faction_id = faction_member.faction_id " .
	"WHERE ruler_id = " . $ruler_id_tmp . " AND requested = 0 AND invited = 0";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if ($ruler_id_tmp == $_SESSION['ruler_id']) {
		print translate('Ruler') . ": <strong id=\"green\">" . formatInput($_GET['name']) . "</strong>" . $resigned . "<br /><br />\r\n";
	} else
		if (isset ($row['faction_id']) && $row['faction_id'] == $faction_id_tmp) {
			print translate('Ruler') . ": <strong id=\"blue\">" . formatInput($_GET['name']) . "</strong>" . $resigned . "<br /><br />\r\n";
		} else {
			print translate('Ruler') . ": <strong id=\"red\">" . formatInput($_GET['name']) . "</strong>" . $resigned . "<br /><br />\r\n";
		}

	if (isset ($row['type_name'])) {
		print (translate($row['type_name']) . ": <a href=\"faction.php?name=" . $row['name'] . "\">" . $row['name'] . "</a><br />\r\n");
	}

	$query = "SELECT count(*) FROM village WHERE ruler_id = " . $ruler_id_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	print translate('Villages') . ": " . $row['count(*)'] . "<br />\r\n";

	print translate("Score") . ": " . number_format($score_tmp) . "<br />\r\n";
	print (translate("Recorded score") . ": " . number_format($last_year_score) . " <small>(Stored at the start of a game year or when the ruler starts playing)</small><br />\r\n");
	print (translate("Growth") . ": $growth<br />\r\n");
	print (translate("Last years growth") . ": $last_year_growth<br /><br />\r\n");
	$query = "SELECT count(*) FROM ruler WHERE score > " . $score_tmp;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	print translate("Rank") . ": " . number_format($row['count(*)'] + 1) . "<br />\r\n";
	print translate("Battle score") . ": " . number_format($battle_score_tmp) . "<br />\r\n";
	if ($battle_score_tmp == 0) {
		print translate("Battle rank") . ": -<br />\r\n";
	} else {
		$query = "SELECT count(*) FROM ruler WHERE experience > " . $battle_score_tmp;
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		print translate("Battle rank") . ": " . number_format($row['count(*)'] + 1) . "<br />\r\n";
	}

	$query = "SELECT score, experience FROM ruler WHERE name = '" . formatInput($_GET['name']) . "'";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$reputation = translate("Unknown");
	$rep_points = $row['experience'] + $row['score'] * 0.001;
	if ($rep_points > 10000000000) {
		$reputation = translate("God of Strife");
	}
	elseif ($rep_points > 1000000000) {
		$reputation = translate("Immortal emperor");
	}
	elseif ($rep_points > 100000000) {
		$reputation = translate("Glorious strategist");
	}
	elseif ($rep_points > 10000000) {
		$reputation = translate("Battle master");
	}
	elseif ($rep_points > 1000000) {
		$reputation = translate("Grand warlord");
	}
	elseif ($rep_points > 100000) {
		$reputation = translate("Field marshal");
	}
	elseif ($rep_points > 10000) {
		$reputation = translate("General");
	}
	elseif ($rep_points > 1000) {
		$reputation = translate("Ambitious officer");
	}
	elseif ($rep_points > 100) {
		$reputation = translate("Skirmisher");
	}
	elseif ($rep_points > 10) {
		$reputation = translate("Scout");
	}
	print (translate("Reputation") . ": $reputation<br />");

	print "<br />" . translate("Started at day") . ": " . number_format($start_turn_tmp) . "<br />\r\n";

	$query = "SELECT turn FROM game";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if ($row['turn'] - $start_turn_tmp == 1) {
		$text = ($row['turn'] - $start_turn_tmp) . " day";
	} else {
		$text = number_format($row['turn'] - $start_turn_tmp) . " " . translate("days");
	}
	print translate("Played for") . " " . $text . "<br />\r\n";
	if ($ruler_id_tmp != $_SESSION['ruler_id']) {
?>
					<br />
					<a href="../diplomacy/send_letter.php?to=<?php print( formatInput( $_GET['name'] ) ); ?>"><strong><?php transprint( 'Send a letter to' ); ?> <?php print( formatInput( $_GET[ 'name' ] ) ); ?></strong></a><br />
<?php
		$buddy = false;
		$query = "SELECT * FROM buddy WHERE ( user_id1 = $other_user_id AND user_id2 = " . $_SESSION['user_id'] . " ) OR ( user_id1 = " . $_SESSION['user_id'] . " AND user_id2 = $other_user_id )";
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if (isset ($row['buddy_id']) && $row['accept1'] == 1 && $row['accept2'] == 1) {
			if ($other_user_id == $row['user_id1'] && $row['ruler1'] == 1) {
				$buddy = true;
			} else
				if ($row['ruler2'] == 1) {
					$buddy = true;
				}
		}

		if ($buddy) {
			print (translate("This ruler is in your <a href=\"../diplomacy/buddylist.php\">buddy list</a>.") . "<br />\r\n");
		} else {
?>			
			<a href="../diplomacy/invite.php?user_id=<?php print( $other_user_id ); ?>"><strong><?php transprint( 'Invite' ); ?> <?php print( formatInput( $_GET['name'] ) ); ?> <?php transprint( 'as a buddy' ); ?></strong></a><br />
<?php		
		}
	}

	$query = "SELECT show_medals, username, user_id " .
			"FROM users " .
			"WHERE user_id = $user_id_tmp";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$no_medals = true;
	$userId = $row[ 'user_id' ];
	if (!isset ($row['username'])) {
		$viewing_username = '';
	} else {
		$viewing_username = $row['username'];
	}

	if ($row['show_medals'] == 1) {
		$query = "SELECT count(*) " .
		"FROM medals " .
		"WHERE user_id = $user_id_tmp";
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if ($row['count(*)'] > 0) {
			$no_medals = false;
		} else {
			$no_medals = true;
		}
	}
?>
				</div> <!-- close blok -->
				<br />
				<div class="blok">
					<a href="../aosmanual/medals.php"><strong><?php transprint( 'Awards and decorations' ); ?></strong></a><br />
<?php


	if (!$no_medals) {
		printMedals($user_id_tmp);
	} else {
?>
					<?php transprint( 'None' ); ?><br />
<?php

	}
?>
				</div>
<?php


	$query = "SELECT admin " .
			"FROM users " .
			"WHERE user_id = " . $_SESSION['user_id'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if ($row['admin'] == 1) {
?>
			<div class="blok">
				<br />
				<strong><?php transprint( 'Give decoration or award' ); ?></strong><br />
				<form method="post" action="award_medal.php">
					<input type="hidden" value="<?php print( $user_id_tmp ); ?>" name="user_id" />
					<input type="checkbox" name="engineer" value="engineer" /> <?php transprint( 'Award engineer branch insignia' ); ?><br />
					<input type="checkbox" name="research" value="research" /> <?php transprint( 'Award research branch insignia' ); ?><br />
					<?php transprint( 'Custom titled decorations or award' ); ?>:<br />
					<input type="text" name="custom" size="60" value="" /><br />
					<input type="submit" value="Confirm" />
				</form><br />
<?php

		if ($_SESSION['ruler_id'] != $ruler_id_tmp) {
?>				
				<br />
				<form method="post" action="../admin/pick_ruler.php">
					<input type="hidden" value="<?php print( formatInput( $_GET['name'] ) ); ?>" name="ruler_name" />
					<input type="submit" value="<?php transprint( 'Take control' ); ?>" />
				</form>
<?php

		}
?>				
			</div> <!-- close blok -->
			<br />
			<div class="blok">
				<strong><?php transprint( 'Additional administrative information' ); ?></strong><br />
				<?php transprint( 'Username' ); ?>: <?php print( $viewing_username ); ?><br />
				<?php transprint( 'User id' ); ?>: <?php print( $user_id_tmp );?><br />
				<?php transprint( 'Ruler id' ); ?>: <?php print( $ruler_id_tmp );?><br />
			</div>
<?php

		$statement = $dal->prepare('SELECT * ' .
				'FROM transactions ' .
				'WHERE user_id = :user_id ' .
				'ORDER BY timestamp DESC ' .
				'LIMIT 25' );
		$statement->bindParam(':user_id', $user_id_tmp, PDO :: PARAM_INT);
		$transactions = $dal->getArrayOfObjects($statement);
		if (count($transactions) > 0) {
?>
				<br />
				<table>
					<tr><th colspan="3">Premium purchases</th></tr>
					<tr>
						<td>Date</td>
						<td>Item</td>
						<td>A gift from someone else</td>
					</tr>
<?php

			$count = 0;
			foreach ($transactions AS $transaction) {
?>
					<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
						<td><?php print( date( 'j F Y', $transaction->timestamp ) ); ?></td>
						<td><?php print( $transaction->item_name ); ?></td>
						<td><?php print( $transaction->from == $transaction->user_id ? 'No' : 'Yes' ); ?></td>
					</tr>
<?php

				$count++;
			}
?>
				</table>
<?php

		}

		$statement = $dal->prepare('SELECT * ' .
				'FROM access_history ' .
				'WHERE user_id = :user_id AND admin_id = -1 ' .
				'ORDER BY timestamp DESC ' .
				'LIMIT 25' );
		$statement->bindParam(':user_id', $user_id_tmp, PDO :: PARAM_INT);
		$accessHistory = $dal->getArrayOfObjects($statement);
		if (count($accessHistory) > 0) {
?>
				<br />
				<table>
					<tr><th colspan="2">Access History</th></tr>
					<tr>
						<td>Date</td>
						<td>Access IP</td>
					</tr>
<?php

			$count = 0;
			foreach ($accessHistory AS $access) {
?>
					<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
						<td><?php print( date( 'G:i:s j F Y', $access->timestamp ) ); ?></td>
						<td><?php print( $access->ip ); ?></td>
					</tr>
<?php

				$count++;
			}
?>
				</table>
<?php

		}

		$statement = $dal->prepare('SELECT access_history.timestamp, users.username ' .
				'FROM access_history ' .
				'JOIN users ON access_history.user_id = users.user_id ' .
				'WHERE admin_id = :admin_id ' .
				'ORDER BY timestamp DESC ' .
				'LIMIT 25' );
		$statement->bindParam( ':admin_id', $userId, PDO::PARAM_INT );
		$accessHistory = $dal->getArrayOfObjects($statement);
		if (count($accessHistory) > 0) {
?>
				<br />
				<table>
					<tr><th colspan="2">Admin control over</th></tr>
					<tr>
						<td>Date</td>
						<td>Control over</td>
					</tr>
<?php

			$count = 0;
			foreach ($accessHistory AS $access) {
?>
					<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
						<td><?php print( date( 'G:i:s j F Y', $access->timestamp ) ); ?></td>
						<td><?php print( $access->username ); ?></td>
					</tr>
<?php

				$count++;
			}
?>
				</table>
<?php
		}

		$statement = $dal->prepare('SELECT access_history.timestamp, users.username ' .
				'FROM access_history ' .
				'JOIN users ON access_history.admin_id = users.user_id ' .
				'WHERE access_history.user_id = :user_id AND admin_id != -1 ' .
				'ORDER BY timestamp DESC ' .
				'LIMIT 25' );
		$statement->bindParam(':user_id', $user_id_tmp, PDO :: PARAM_INT);
		$accessHistory = $dal->getArrayOfObjects($statement);
		if (count($accessHistory) > 0) {
?>
				<br />
				<table>
					<tr><th colspan="2">Admin control</th></tr>
					<tr>
						<td>Date</td>
						<td>By</td>
					</tr>
<?php

			$count = 0;
			foreach ($accessHistory AS $access) {
?>
					<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
						<td><?php print( date( 'G:i:s j F Y', $access->timestamp ) ); ?></td>
						<td><?php print( $access->username ); ?></td>
					</tr>
<?php

				$count++;
			}
?>
				</table>
<?php

		}

	}
	unset ($ruler_id_tmp);
	unset ($score_tmp);
	unset ($battle_score_tmp);
} else {
	print (htmlentities($_GET['name']) . " " . translate("doesn't exist") . ".");
}
aos_footer();
?>
